#include <SDL.h>
#include "controller.h"

Controller::~Controller() {
  atexit(SDL_Quit);
}

void Controller::play() {
  SDL_Event event;

  bool done = false;
  bool keyCatch = true;
  while ( ! done ) {
	if(!model.isPaused()){
		model.draw();
		model.update();
	}
    SDL_PollEvent(&event);
    Uint8 *keystate = SDL_GetKeyState(NULL);
    switch (event.type) {
      case SDL_QUIT: {
        done = true;
        break;
      }
      case SDL_KEYUP: {
        keyCatch = true;
      }
      case SDL_KEYDOWN: {
        if (keystate[SDLK_q] || keystate[SDLK_ESCAPE]) {
          done = true;
        }
        if ( keystate[SDLK_F1] && keyCatch) {
		  model.toggleHelp();
          keyCatch = false;
        }
        if ( (keystate[SDLK_x] ||keystate[SDLK_SPACE] )&& keyCatch) {
          model.changeTracker();
          keyCatch = false;
        }
        if ( (keystate[SDLK_p] )&& keyCatch) {
          model.pause();
          keyCatch = false;
        }
        break;
      }
    }
  }
}
